<extend name="layout/main" />

<block name="HEADER">
	<style type="text/css">
		.layui-layer-page .layui-layer-content{
			overflow: initial;
		}
		#grid .layui-table-view .layui-table td .layui-table-cell{
			height: 36px;
		    line-height: 36px;
		}
		#grid .layui-btn+.layui-btn {
		    margin-left: 0;
		}
		.xw-layui-layer .layui-layer-content{
			padding: 0;
		}
		.xw-form .xw-form-formType-div{
			height: 100%;
		}
	</style>
</block>

<block name="CONTENT">
	<div class="wrapper">
		<div class="panel-heading">
			<control:search id="searchs" />
		</div>
		<div class="content">
			<control:grid id="grid" padding="0" height="100%" />
		</div>
	</div>

	<script id="tplExcelImport" type="text/html">
		<div id='excelImportFrom'></div>
	</script>
</block>

<block name="FOOTER">
	<script type="text/javascript">
		var page = (function(ns){

		    //页面初始化
		    ns.init=function(){
		        ns.query();
		    }

		    //搜索事件
		    ns.query=function(){
		    	grid.load( searchs.getData() );
		    }

		    //新增
		    ns.add=function(){
		    	var filter = {
		            url:"modules/rebate/discount/edit",
		            oid:"",
		            mode:1
		        }
		        $.openIframe(filter, "新增",['850px', '92%']);
		    }

		    //编辑
		    ns.edit=function(){
		    	var selectedRow = grid.getSelectedRows()[0];
		        var filter = {
		            url:"modules/rebate/discount/edit",
                    oid: selectedRow.id ,
                    mode:2
		        }
		        $.openIframe(filter, "编辑",['850px', '92%']);
		    }

		    //删除
		    ns.del =function(){
		    	if( !grid.isSelected() ){
		    		return false;
		    	}
		    	$.confirm("确定删除？", function(){
		    		$.ajaxs('/admin/v1/activity_discount/del',{
			    		ids:grid.getSelectedColumnValue('id')
			    	},function(res){
			    		$.toast(1,"已删除！");
			    		grid.load();
			    	});
		    	});
		    }

		    //下拉移动  type: 1 = 上移 ,  type: 2 = 下移
		    ns.move = function(type){
		    	grid.adjustSort(type,"sort_no");
		    }

		    //启用禁用
		    ns.setEnable =function(state){
		    	if( !grid.isSelected() ){
		    		return false;
		    	}
		    	$.confirm("确定" + (state == "0" ? "禁用?" : "启用?"),function(index){
			    	$.ajaxs("/admin/v1/Spread/setEnable",{
			    	 	ids: grid.getSelectedColumnValue("id"),
			    	 	state: state
			    	},function(res){
			            grid.load();
			        });
			        layer.close(index);
		        });
		    }

		    //作废
			ns.cancel = function(status){
                var selectedRow = grid.getSelectedRows()[0];
                if(!selectedRow) return $.toast(0,"请先选择满减满折活动!");

                $.ajaxs('/admin/v1/activity_discount/setStop',{
                    ids: grid.getSelectedColumnValue("id"),
                    state: status
                }).then(function(res){
                    $.toast(1,'操作成功！');
                    grid.load();
                    $.close();
                });
			}

		    //查看商户
		    ns.seeStore =function(state){
		    	if( !grid.isSelected() ){
		    		return false;
		    	}
		        var filter = {
		            url:"modules/coupon/release/seeStore",
		            oid: grid.getSelectedColumnValue("store_ids"),
		            mode:2
		        }
		        $.openIframe(filter, "所属商户",['800px', '520px']);
		    }

		    //查看发放明细
		    ns.seeGrantDetail =function(id){
		    	if( !grid.isSelected() ){
		    		return false;
		    	}
		        var filter = {
		            url:"modules/coupon/release/grantDetail",
		            oid: id,
		            mode:2,
		            is_repeat: grid.getSelectedColumnValue("is_repeat")
		        }
		        $.openIframe(filter, "发放明细",['1100px', '90%']);
		    }

		    //预览海报
		    ns.generatePoster =function(id){
		    	layer.photos({
		    		anim: 5 ,
				    photos: {
						data: [{
					    	"src": __root__+"/index.php/admin/v1/coupon/generatePoster?cid="+id
						}]
				    }
				});
		    }



		    //编辑数量
		    ns.editNum =function(id){
		    	var selectedRows = grid.getSelectedRows(),stockForm;
		    	if( !selectedRows[0] ){
		    		$.toast(0,"请选择编辑数量的优惠券数据！");
		    		return false;
		    	}
		   		function successFn(){
		    		stockForm = layui.forms.render("stockForm",'',{ mode:2,oid: selectedRows[0].id });
		    		function callbackFn(){
		    			//总数量
		    			var total = stockForm.getValue('total_count_hidden');
		    			var add = stockForm.getValue('add_num');
		    			var reduce = stockForm.getValue('reduce_num');
		    			stockForm.setValue('total_num', Number(total) + add - reduce);
		    			//剩余数量
		    			var total_num = stockForm.getValue('total_num');
		    			var grant_num = stockForm.getValue('grant_num');
		    			stockForm.setValue('remain_num', total_num -  Number(grant_num) );
		    		}
		    		stockForm.on("input",'add_num',callbackFn);
		    		stockForm.on("input",'reduce_num',callbackFn);
		    	}
		    	function yesFn(){
		    		var param = stockForm.getData();
		    		if( stockForm.getValue('remain_num') < 0) return $.toast(0,'可用库存不能小于0！');
		    		if(param){
		    			$.ajaxs("/admin/v1/coupon/setNum",param,function(res){
			                $.toast(1,res.msg);
			                grid.load();
			                layer.closeAll();
			            });
		    		}
		        }
		    	var html = "<div id='stockForm'></div>";
		        $.openModalDialog(html,"数量编辑",successFn,yesFn,["350px","330px"]);
		    }


		    return ns;
		}({}));


		$(function(){ page.init();  });

	</script>
</block>